package com.nowcoder.code.lcof;

import java.util.ArrayList;
import java.util.Collections;

public class JZ45 {

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList();

        list.add("39");
        list.add("397");
        list.add("6");

        Collections.sort(list, (a, b) -> new JZ45().compare(a, b));

        System.out.println(list);

    }


    public String PrintMinNumber(int[] numbers) {
        ArrayList<String> list = new ArrayList<>(numbers.length);

        for (Integer i : numbers) {
            list.add(i.toString());
        }
        list.sort(this::compare);

        StringBuilder sb = new StringBuilder();
        for (String s : list) {
            sb.append(s);
        }

        return sb.toString();
    }


    public int compare(String a, String b) {
        char[] as = a.toCharArray();
        char[] bs = b.toCharArray();
        int ai = 0, bi = 0;
        while (ai < as.length || bi < bs.length) {
            char ac = ai >= as.length ? as[0] : as[ai];
            char bc = bi >= bs.length ? bs[0] : bs[bi];

            int r = ac - bc;
            if (r != 0) {
                return r;
            }
            ai++;
            bi++;
        }
        return 0;
    }


}
